package com.dacong.nezha.utils;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ExcuteHttp {
    public static String baseUrl = "https://cdn.smartvv.com:7777/api";

    public static void main(String[] args) {
        String json = "{\"url\":\"/assetFlow/list\",\"options\":{\"method\":\"POST\",\"body\":\"{\\\"page\\\":0,\\\"size\\\":10,\\\"flowType\\\":\\\"下单交易\\\",\\\"beginTime\\\":1740585600000,\\\"endTime\\\":1740671999999}\",\"headers\":{\"Content-Type\":\"application/json\",\"Authorization\":\"Bearer eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiJjNDIyNWEzYThhMjU0MjVmYWMyMTkwOGM4MDFiZmVkZiIsInVzZXIiOiJ4eTg4NjY5NiIsInN1YiI6Inh5ODg2Njk2In0.wm2dnwDd2eRDMbejqpmuOjJehRQzMpOeiX4R_8c3q3wZKjspaptHkRbtnTep2WA0WL528bgofvGHDQWeNgJfVg\",\"Language\":\"zh-CN\"}}}";
        String s = executeHttp(json);
        System.out.println(s);
    }

    public static String login(String username,String password,String code){
        String body ="{\n" +
                "    \"url\": \"/auth/login\",\n" +
                "    \"options\": {\n" +
                "        \"method\": \"POST\",\n" +
                "        \"body\": \"{\\\"loginCode\\\":\\\""+username+"\\\",\\\"password\\\":\\\""+AesUtil.rsa(password)+"\\\",\\\"authenticatorCode\\\":\\\""+code+"\\\",\\\"machineCode\\\":\\\"21dad7e8502a6253495d072b0ca1383163fbd187abfe628c326f5fe96d9c0d81+74:56:3c:cf:9e:53\\\",\\\"version\\\":\\\"2.11.0\\\"}\",\n" +
                "        \"headers\": {\n" +
                "            \"Content-Type\": \"application/json\",\n" +
                "            \"Language\": \"zh-CN\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        return executeHttp(body);
    }

    public static String executeHttp(String body){
        String encrypt = AesUtil.encrypt(body);
        HttpResponse response = HttpUtil.createPost(baseUrl)
                .body(encrypt)
                .header("sec-ch-ua-platform","\"Windows\"")
                .header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) tmcos25/2.11.02 Chrome/104.0.5112.81 Electron/20.0.2 Safari/537.36")
                .execute();
        if (response.isOk()){
            String s = AesUtil.decryptStr(response.body());
            return s;
        }else{
            log.info("code:{} body:{}",response.getStatus(),response.body());
            String s = "错误码："+response.getStatus();
            throw new RuntimeException(s);
        }

    }

    public static String executeHttpForLog(String body){
        try {
            String encrypt = AesUtil.encrypt(body);
            HttpResponse response = HttpUtil.createPost(baseUrl)
                    .body(encrypt)
                    .header("sec-ch-ua-platform","\"Windows\"")
                    .header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) tmcos25/2.11.0 Chrome/104.0.5112.81 Electron/20.0.2 Safari/537.36")
                    .execute();
            if (response.isOk()){
                String s = AesUtil.decryptStr(response.body());
                JSONObject entries = JSONUtil.parseObj(s);

                if (entries.getInt("code") != null && entries.getInt("code")== 10) {
                    log.info("触发验证码");

                }
                return s;
            }
            System.out.println("请求失败");
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    public static HttpResponse executeHttpForResp(String body){
        String encrypt = AesUtil.encrypt(body);
        HttpResponse response = HttpUtil.createPost(baseUrl)
                .body(encrypt)
                .header("sec-ch-ua-platform","\"Windows\"")
                .header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) tmcos25/2.11.0 Chrome/104.0.5112.81 Electron/20.0.2 Safari/537.36")
                .execute();
        return response;
    }

}
